﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace WsGameFrame
{
    public class TransCoding : BaseSinglton<TransCoding>
    {

        #region  MD5相关

        /// <summary>
        /// 获取字符串MD5
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string GetStringMD5Encrypt(string value)
        {
            byte[] result = Encoding.Default.GetBytes(value);    //tbPass为输入密码的文本框
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            string md5s = BitConverter.ToString(output).Replace("-", "");  //tbMd5pass为输出加密文本的文本框
            md5s = md5s.ToLower();
            return md5s;
        }

        /// <summary>
        /// 获取文件MD5
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string GetFileMD5Encrypt(string filePath)
        {
            try
            {
                FileStream file = new FileStream(filePath, FileMode.Open);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                string md5s = BitConverter.ToString(retVal).Replace("-", "");  //tbMd5pass为输出加密文本的文本框
                md5s = md5s.ToLower();
                return md5s;
            }
            catch (Exception ex)
            {
                throw new Exception("获取文件MD5失败:" + ex.Message);
            }
        }


        #endregion

        #region base64

        ///<summary>
        ///Base64加密
        ///</summary>
        ///<paramname="Message"></param>
        ///<returns></returns>
        public string Base64Code(string Message)
        {
            byte[] bytes = Encoding.Default.GetBytes(Message);
            return Convert.ToBase64String(bytes);
        }


        public string Base64Decode(string Message)
        {
            byte[] bytes = Convert.FromBase64String(Message);
            return Encoding.Default.GetString(bytes);
        }


        #endregion


    }
}
